package com.example.administrator.myapplication.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.adapter.RecordAdapter;
import com.example.administrator.myapplication.base.BaseMainActivity;
import com.example.administrator.myapplication.mode.RecordData;
import com.example.administrator.myapplication.mode.RecordDataManager;
import com.example.administrator.myapplication.utils.divider.HorizontalDividerItemDecoration;

import java.util.ArrayList;

import butterknife.BindView;

import static com.example.administrator.myapplication.mode.RecordDataManager.getData;


/**
 * 说明：记录列表
 * 作者： 阳2012; 创建于： 2017/5/3 7:17
 */
public class RecordActivity extends BaseMainActivity implements RecordDataManager.RefreshListener {
    @BindView(R.id.switchRoot)
    RecyclerView switchRoot;
    private ArrayList<RecordData> mDatas = new ArrayList<>();
    RecordAdapter adapter;

    public static void startUI(Activity activity) {
        Intent intent = new Intent(activity, RecordActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public int getContentView() {
        return R.layout.activity_record;
    }


    @Override
    public void initView() {
        super.initView();
        toolbar.setTitle("记录我的足迹");

        adapter = new RecordAdapter(this, mDatas);

//        mDatas.add(new RecordData("https://www.baidu.com", "2017-5-6"));
        mDatas.addAll(getData());
        switchRoot.addItemDecoration(new HorizontalDividerItemDecoration.Builder(this).colorResId(R.color.gray).sizeResId(R.dimen.dp_1).build());
        switchRoot.setLayoutManager(new LinearLayoutManager(this));
        switchRoot.setAdapter(adapter);

        RecordDataManager.getInstands().setRefreshListener(this);
    }

    @Override
    public void parseIntent(Intent intent) {

    }

    @Override
    public int currentItem() {
        return 1;
    }

    @Override
    public void refreshCallback(boolean isRefresh) {
        mDatas.clear();
        ArrayList<RecordData> data = getData();

        if (data.size() > 0) {
            mDatas.addAll(data);
            adapter.notifyDataSetChanged();
        } else {
            RecordData d = new RecordData();
            d.setContent("暂无数据");
            mDatas.add(d);
            adapter.notifyDataSetChanged();
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        refreshCallback(true);

    }
}
